package com.hymjweb.demo.hymj.customer_manage.contact.controller;

import com.hymjweb.demo.framework.result.BaseResultMap;
import com.hymjweb.demo.framework.sys.BaseController;
import com.hymjweb.demo.framework.sys.SysConstant;
import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.customer_manage.contact.bean.BCustomerContactPojo;
import com.hymjweb.demo.hymj.customer_manage.contact.service.BCustomerContactService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/pages/customer_manage/contact")
public class BCustomerContactController extends BaseController {

    @Autowired
    private BCustomerContactService bCustomerContactService;

    /**
     * 根据客户id，查询对应的客户联系人
     * @param map
     * @return
     */
    @RequestMapping("/queryContactByCustomerId")
    @ResponseBody
    public List<BCustomerContactPojo> queryContactByCustomerId(@RequestBody(required = true) Map map){
        if(map!=null && map.size()>0){
            String customerId=DataTypeUtil.toNotNullString(map.get("customerId"));
            List<BCustomerContactPojo> contactPojos = bCustomerContactService.queryContactByCustomerId(customerId);
            return contactPojos;
        }
        return null;
    }


    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(@RequestBody(required = true) String id) throws Exception {
        if(DataTypeUtil.isEmptyStr(id)){
            throwException("客户联系人删除参数错误!");
        }
        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("客户联系人删除成功!");

        int  affectRows = bCustomerContactService.delete(id);

        if (affectRows <= 0) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("客户联系人删除失败!");
        }

        return baseResultMap;
    }

    @RequestMapping("/insert")
    @ResponseBody
    public Object insert(@RequestBody(required = true) BCustomerContactPojo bCustomerContactPojo) throws Exception {
        if(bCustomerContactPojo==null){
            throwException("客户联系人添加参数错误!");
        }

        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("客户联系人添加成功!");

        int affectRows = bCustomerContactService.insert(bCustomerContactPojo);
        if (affectRows <= SysConstant.INT_ZERO) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("客户联系人添加失败!");
        }


        return baseResultMap;
    }


    @RequestMapping("/checkUsed")
    @ResponseBody
    public Object checkUsed(@RequestBody(required = true)  BCustomerContactPojo bCustomerContactPojo) throws Exception {
        if(bCustomerContactPojo==null){
            throwException("客户联系人检测参数错误!");
        }

        BaseResultMap baseResultMap = new BaseResultMap();

        Object object = bCustomerContactService.checkUsed(bCustomerContactPojo);
        if(object!=null){
            baseResultMap.setOk(true);
            baseResultMap.setMessage("该客户联系人使用中!");
        }
        else{
            baseResultMap.setOk(false);
            baseResultMap.setMessage("该客户联系人未使用!");
        }

        return baseResultMap;
    }


}
